import { Component, OnInit, Injector } from '@angular/core';
import { FormGroup, FormBuilder, Validators } from '@angular/forms';

import { SocialService } from '@delon/auth';

import { LoginService } from './login.service';
import { AppComponentBase } from '@abp/components';

@Component({
    templateUrl: './login.component.html',
    styleUrls: ['./login.component.less'],
    providers: [SocialService],
})
export class UserLoginComponent extends AppComponentBase {
    validateForm: FormGroup;
    submitting: boolean = false;

    constructor(
        injector: Injector,
        private fb: FormBuilder,
        public loginService: LoginService
    ) {
        super(injector);
    }

    ngOnInit(): void {
        this.validateForm = this.fb.group({
            email: ['',[Validators.required]],
            password: ['', [Validators.required]],
        });
    }


    submitForm(): void {
        for (const key in this.validateForm.controls) {
            this.validateForm.controls[key].markAsPristine();
            this.validateForm.controls[key].updateValueAndValidity();
        }
        if(this.validateForm.valid)
        {
            this.login();
        }
    }


    login(): void {
        this.submitting = true;
        this.loginService.authenticate(
            () => this.submitting = false,"/dashboard",() => this.submitting = false,
        );
    }

    get isSelfRegistrationAllowed(): boolean {
        return true;    //TODO: 是否允许注册判断
    }

    forgetPassword(){
        this.message.warn("功能正在开发中，请联系管理员修改密码！")
    }
}
